package com.thkj.business.banner.controller;

import com.thkj.business.banner.entity.TblBanner;
import dp.R;
import dp.dao.PagingList;
import com.thkj.framework.base.BaseController;

import com.thkj.business.banner.service.TblBannerService;
import com.thkj.util.RequestUtil;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.sql.Timestamp;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

@RestController
@RequestMapping("/backstage/banner")
public class BannerController extends BaseController {
    @Autowired
    TblBannerService tblBannerService;

    @RequestMapping("")
    public ModelAndView index() {
        return new ModelAndView("banner/list");
    }

    @RequestMapping("/query")
    public R query(HttpServletRequest request, int page, int size) {
        Map<String, Object> param = RequestUtil.getParameterMap(request);
        PagingList<TblBanner> list = tblBannerService.query(page, size, param);
        return R.success(list);
    }

    @RequestMapping("/edit")
    public R edit(TblBanner banner) {
        try {
            if (StrUtil.isBlank(banner.getId())) {
                banner.setId(IdUtil.objectId());
                banner.setBannerUrl(banner.getBannerUrl());
                banner.setCreateTime(new Timestamp(System.currentTimeMillis()));
                tblBannerService.insertTblBanner(banner);
            } else {
                tblBannerService.updateTblBanner(banner);
            }
            return R.SUCCESS;
        } catch (Exception e) {
            return R.failure(e.getMessage());
        }
    }

    @RequestMapping("/delete")
    public R delete(String id) {
        tblBannerService.deleteTblBanner(id);
        return R.SUCCESS;
    }
}